'''
Created on Jul 22, 2010

@author: behrooz
'''
from amscms.core.utils.request import render_to_response
from amscms.reports import gviz_api
from django.utils.safestring import mark_safe

class ChartType:
    ColumnChart = "ColumnChart"
    BarChart = "BarChart"
    LineChart = "LineChart"
    PieChart = "PieChart"
    AnnotatedTimeLine = "AnnotatedTimeLine"

def render_to_chart(request, columns_desc, chart_desc, data, revdata=None):
    context = {}
    rev_columns_desc = None
    rev_chart_desc = None
    
    if revdata is not None:
        rev_columns_desc = {}
        for k, v in columns_desc.items():
            rev_columns_desc[k] = (v[0], v[1][::-1])
        rev_chart_desc = {}
        for k, v in chart_desc.items():
            rev_chart_desc[k] = v[::-1]
        rev_table = gviz_api.DataTable(rev_columns_desc)
        rev_table.LoadData(revdata)
        rev_json_data = rev_table.ToJSon()
        context["revdata"] = mark_safe(rev_json_data)
        context["revChartDesc"] = rev_chart_desc
    
    table = gviz_api.DataTable(columns_desc)
    table.LoadData(data)
    json_data = table.ToJSon()
    context["data"] = mark_safe(json_data)
    context["chartDesc"] = chart_desc
    
    return render_to_response(request, "report.html", context)

